package service;

import dao.BaseInfoDao;
import dao.DepartmentDao;
import dao.WorkerDao;
import dao.impl.BaseInfoDaoImpl;
import dao.impl.DepartmentDaoImpl;
import dao.impl.WorkerDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import pojo.FindWorkerParam;
import pojo.Worker;
import util.LogUtil;
import util.Result;
import util.StringDataCheckUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class BaseInfoService {

    private BaseInfoDao dao = new BaseInfoDaoImpl();
    private WorkerDao workerDao = new WorkerDaoImpl();
    private DepartmentDao departmentDao = new DepartmentDaoImpl();

    /**
     * 展示所有未删除的非超级管理员的员工
     *
     * @param request
     * @param response
     */
    public void workerList(HttpServletRequest request, HttpServletResponse response) {
        String pageStr = request.getParameter("page");
        String limitStr = request.getParameter("limit");
        // 数据验证
        if (!StringDataCheckUtil.check(pageStr, limitStr)) {
            Result.response(1001, "数据参数有误！", response);
            return;
        }
        Integer page = pageStr == null ? 1 : Integer.valueOf(pageStr);
        // 会员数据
        List<Worker> workerList = dao.workerList(page, Integer.valueOf(limitStr));
        // 总的条数
        Integer count = dao.count();
        Result.response(0, "获取数据成功！", count, response, workerList);
    }

    /**
     * 删除员工
     *
     * @param request
     * @param response
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        Boolean res = false;
        // 遍历ids
        for (String id : ids) {
            res = dao.delete(Integer.valueOf(id)) > 0 ? true : false;
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);
    }

    /**
     * 展示编辑员工的信息
     *
     * @param request
     * @param response
     */
    public void showEditWorker(HttpServletRequest request, HttpServletResponse response) {
        try {
            String workerId = request.getParameter("workerId");
            // 验证数据
            if (!StringDataCheckUtil.check(workerId)) {
                Result.response(1001, "数据有误！", response);
                return;
            }
            // 通过id查找员工
            Worker worker = dao.findWorkerById(Integer.valueOf(workerId));
            // 将员工信息保存在session中
            Result.response(200, "查找成功！", response, worker);
            request.getSession().setAttribute("editWorker", worker);
            request.getRequestDispatcher("/WEB-INF/pages/baseInfo/workerManage/workerEdit.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑员工的信息
     *
     * @param request
     * @param response
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取前台数据
            Map<String, String[]> parameterMap = request.getParameterMap();
            Worker worker = new Worker();
            BeanUtils.populate(worker, parameterMap);
            // 判断输入的编辑信息是否重复
            Integer checkEditInfo = dao.checkEditInfo(worker);
            if (checkEditInfo == 1){
                Result.response(1002, "该手机号已存在！", response);
                return;
            }else if (checkEditInfo == 2){
                Result.response(1003, "该身份证号码已经存在！", response);
                return;
            }
            worker.setWorkerUpdateTime(new Timestamp(System.currentTimeMillis()));
            if (dao.edit(worker) > 0) {
                LogUtil.addLog("编辑员工["+worker.getWorkerName()+"]的信息成功", request);
                Result.response(200, "编辑成功！", response);
                return;
            }
            LogUtil.addLog("编辑员工["+worker.getWorkerName()+"]的信息失败", request,0);
            Result.response(1001, "编辑失败!", response);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据条件查找员工
     * @param request
     * @param response
     */
    public void findWorkerByParam(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取前端查询条件
            Map<String, String[]> parameterMap = request.getParameterMap();
            FindWorkerParam findWorkerParam = new FindWorkerParam();
            BeanUtils.populate(findWorkerParam, parameterMap);
            List<Worker> workByParam = workerDao.findWorkByParam(findWorkerParam);
            if (workByParam == null){
                Result.response(1001, "查找失败！" ,response);
                return;
            }
            Result.response(0, "查找成功！", workerDao.countFindWorkByParam(findWorkerParam), response, workByParam);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    /**
     * 真删  员工
     * @param request
     * @param response
     */
    public void realDelete(HttpServletRequest request, HttpServletResponse response) {
        String[] ids = request.getParameterValues("ids[]");
        Boolean res = false;
        // 遍历ids
        for (String id : ids) {
            res = dao.realDelete(Integer.valueOf(id)) > 0 ? true : false;
        }
        if (res) {
            Result.response(200, "删除成功!", response);
            return;
        }
        Result.response(1001, "删除失败!", response);

    }

    /**
     * 恢复员工
     * @param request
     * @param response
     */
    public void restore(HttpServletRequest request, HttpServletResponse response) {
        String restoreId = request.getParameter("restoreId");
        Boolean res = false;
        res = dao.restore(Integer.valueOf(restoreId)) > 0 ? true : false;
        if (res) {
            Result.response(200, "恢复成功!", response);
            return;
        }
        Result.response(1001, "恢复失败!", response);
    }
}
